package com.huaxiafinance.management.util;


import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class SMSUtils
{
    /**
     * 短信发送
     */
    private static final Logger log = Logger.getLogger(SMSUtils.class);

    private static final String url = "http://sdk4rptws.eucp.b2m.cn:8080/sdkproxy/sendsms.action";

    /**
     * 同一个号码发短信的时间间隔
     */
    private static final double TIMEINTERVAL = 60;

    private static final String CDKEY = "6SDK-EMY-6688-JCTTN";

    private static final String PASSWORD = "635610";

    private static final String SEQID = "123213";

    private static final String SMSPRIORITY = "1";

    /**
     * 短信通道2
     */
    private static final String SMS_URL = "http://sh2.ipyy.com/sms.aspx";

    public static boolean sendSMS(String mobile, String message)
    {
        return true;
    }

    private static boolean sendSMSByYiMei(String mobile, String message)
    {
        try
        {
            if (mobile == null || "".equals(mobile) || message == null || "".equals(message))
            {
                return false;
            }
            String res = sendSMSActionYiMei(url, mobile, message);
            Pattern p = Pattern.compile("<error>.*</error>");
            Matcher m = p.matcher(res);
            while (m.find())
            {
                res = m.group();
                res = res.replace("<error>", "");
                res = res.replace("</error>", "");
                break;
            }
            if ("0".equals(res))
            {
                return true;
            }
        }
        catch (Exception e)
        {
            log.error("错误++++++" + e);
            return false;
        }
        return false;
    }

    private static boolean sendSMS165(String mobile, String message)
    {
        try
        {
            log.info("电话：：" + mobile + "短信消息：：" + message);
            if (mobile == null || "".equals(mobile) || message == null || "".equals(message))
            {
                return false;
            }
            String res = sendSMSAction165(SMS_URL, mobile, message);
            log.info("短信返回消息：：" + res);

            Pattern p = Pattern.compile("<returnstatus>.*</returnstatus>");
            Matcher m = p.matcher(res);
            while (m.find())
            {
                res = m.group();
                res = res.replace("<returnstatus>", "");
                res = res.replace("</returnstatus>", "");
                break;
            }
            if ("Success".equals(res))
            {
                return true;
            }
        }
        catch (Exception e)
        {
            log.error("错误++++++" + e);
            return false;
        }
        return false;
    }

    /**
     * cdkey 用户序列号。 password 用户密码 phone 手机号码（最多200个），多个用英文逗号(,)隔开。 message
     * 短信内容（UTF-8编码）（最多500个汉字或1000个纯英文）。 addserial 附加号（最长10位，可置空）。 seqid 长整型值企业内部必须保持唯一，获取状态报告使用
     * smspriority 短信优先级1-5 6SDK-EMY-6688-JCTTN 密码：635610 签名： 【华夏信财】
     *
     * @param url
     * @return String
     * @throws Exception
     */
    private static String sendSMSActionYiMei(String url, String phone, String message)
        throws Exception
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("cdkey", CDKEY);
        params.put("password", PASSWORD);
        params.put("phone", phone);
        params.put("message", message);
        params.put("seqid", SEQID);
        params.put("smspriority", SMSPRIORITY);
        String result = HttpClientUtil.post(url, params);
        return result;
    }

    private static String sendSMSAction165(String url, String phone, String message)
        throws Exception
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userid", "");
        params.put("account", "jksc520");
        params.put("password", "226845");
        params.put("mobile", phone);
        params.put("content", message + "【华夏信财】");
        params.put("sendTime", "");
        params.put("action", "send");
        params.put("extno", "");
        return HttpClientUtil.post(url, params);
    }

    public static String getRandNum(int charCount)
    {
        String charValue = "";
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < charCount; i++ )
        {
            char c = (char)(randomInt(0, 10) + '0');
            stringBuffer.append(String.valueOf(c));
        }
        charValue = stringBuffer.toString();
        return charValue;
    }

    public static int randomInt(int from, int to)
    {
        Random r = new Random();
        return from + r.nextInt(to - from);
    }
}